package multimanipulators.finders;

import java.io.OutputStreamWriter;

import multimanipulators.converters.simple.Converter_tagToString;
import multimanipulators.interfaces.Converters;
import multimanipulators.interfaces.Finders;
/**
 * @author Lieven Roegiers
 * @copyright 2009
 * @project javamylibs
 * @from http://javamylibs.googlecode.com/svn/trunk/
**/
public class Finder implements Finders{
	private Converters converter;
	public String tofind;
	//private myfinder rec;
	public int recklok;
	public void init (){
		recklok=0;
	}
	public Finder(String findstring, Converters converter) {
		this.converter= converter;
		tofind = findstring;
		init();
	}
	public Finder(String findstring) {
		this(findstring,null);
	}
	public boolean ismatchfound(){
		return tofind.length()== recklok;
	}
	public String toString() {
		return new String("<FOUND:'"+tofind+"' "+tofind.length()+" RecLengt:'"+recklok+"'>");
	}
	public String getTofind(){
		return tofind;
	}
	public boolean isnext(int achar){
		try{
			return(isvalid((char)achar));
		}catch(StringIndexOutOfBoundsException e){
			init();
			return false;
		}
	}
	private boolean isvalid(char achar){
		if(achar == tofind.charAt(recklok)){
			recklok++;
			return true;
		}else{
			recklok = 0;
			return false;
		}
	}
	public void setConverter(Converters converter) {
		this.converter= converter;
	}
	public void convert(char[] data, OutputStreamWriter doutstream) throws Exception {
		converter.manipulate(data, doutstream);
	}
	@Override
	public int compareTo(Object o) {
		return 0;// TODO Auto-generated method stub
	}
	@Override
	public int getusedbufferlength() {
		return recklok;
	}
}
